package com.ybn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ybn.config.HttpClientUtil;
import com.ybn.config.WxApiConfig;
import com.ybn.entity.VoteUser;
import com.ybn.service.Impl.VoteUserServiceImpl;
import com.ybn.service.VoteUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("vote_user")
public class VoteUserController {
    @Resource
    VoteUserServiceImpl voteUserService;

    @RequestMapping("/dologin")
    public String doLogin() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(String vuuserid, String vupassword, HttpServletRequest request) {
        QueryWrapper<VoteUser> qw = new QueryWrapper<>();
        qw.eq("VU_USER_ID", vuuserid);
        qw.eq("VU_PASSWORD", vupassword);
        VoteUser voteUser = voteUserService.getOne(qw);
        if (voteUser != null) {
            if (voteUser.getVustatus() == 1) {
                request.setAttribute("errorMsg", "该账号已被禁用，请联系管理员！");
                return "login";
            } else {
                request.getSession().setAttribute("userinfo", voteUser);
            }

        } else {
            request.setAttribute("errorMsg", "用户名或密码错误！");
            return "login";
        }
        return "redirect:/vote_subject/index";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    public String save(VoteUser voteUser) {
        boolean save = voteUserService.save(voteUser);
        return "redirect:/vote_user/login";
    }


    @RequestMapping("/test")
    @ResponseBody
    @CrossOrigin
    public Map<String, Object> test(String username, String password) {
        System.out.println(username);
        System.out.println(password);
        Map map = new HashMap();
        map.put("test", "test");
        return map;
    }

    /**
     * 微信一键登录
     * **/
    @RequestMapping("/wxlogin")
    @ResponseBody
    @CrossOrigin//跨域访问
    public VoteUser wxlogin(String code){
//        System.out.println(WxApiConfig.WXLOGIN_jscode2session+"&js_code="+code);
        String result = HttpClientUtil.doGet(WxApiConfig.WXLOGIN_JSCODE2SESSION+"&js_code="+code);
        //转换为json对象
        String openid = new JSONObject(result).getString("openid");
        System.out.println("openid"+openid);

//        String openid = (new JSONObject(HttpClientUtil.doGet(WxApiConfig.WXLOGIN_jscode2session+"&js_code="+code))).getString("openid");
//        System.out.println("openid"+openid);

        QueryWrapper<VoteUser> qw = new QueryWrapper<>();
        qw.eq("openid", openid);
        VoteUser voteUser = voteUserService.getOne(qw);
        if(voteUser==null){
            voteUser=new VoteUser();
            voteUser.setOpenid(openid);
            voteUser.setVuuserid(openid);
            voteUser.setVuusername("微信用户"+ UUID.randomUUID().toString().substring(0,10));
            voteUser.setVustatus(2);
            voteUser.setVuversion(1);
            voteUser.setVupassword("");
            voteUserService.save(voteUser);
        }
        return voteUser;
    }















//    @Resource
//    VoteUserService voteUserService;
//
//    @RequestMapping("/login")
//    public String login(String vuuserid, String vupassword, HttpSession session){
//        VoteUser voteUser = voteUserService.selectUserByUserName(vuuserid,vupassword);
//        if (voteUser != null) {
//            session.setAttribute("voteUser", voteUser);
//            return "redirect:/index"; // 跳转到主页面
//        }
//        return  "/login"; //"/WEB-INF/login.jsp";
//    }
//
//
//    @RequestMapping("/register")
//    public String register(){
//        return "register";
//    }
//
//    @PostMapping("/ ")
//    public String register(VoteUser voteUser){
//        String vuUserId = voteUser.getVuuserid();
//        QueryWrapper<VoteUser> qw = new QueryWrapper<>();
//        qw.eq("VU_USER_ID",vuUserId);
//        VoteUser user = voteUserService.getOne(qw);
//        if(user==null){
//            voteUser.setVustatus(1);
//            voteUser.setVustatus(1);
//            boolean save = voteUserService.save(voteUser);
//            if(save){
//                return "login";
//            }else{
//                return "register";
//            }
//        }else{
//            return "register";
//        }
//    }
}
